//
//  ChatRoomViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/6.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ChatRoomViewController.h"
#import "ChatRoomModel.h"
#import "ChatRoomTableViewCell.h"
#import "MyChatViewController.h"
#import "ChatRoomSearchViewController.h"

@interface ChatRoomViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *chatRoomTableView;
@property (nonatomic, strong) NSMutableArray *chatRoomDataArray;

@end



@implementation ChatRoomViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi修改
    [self changeNavigation];
    // 数据源
    [self addData];
    //  布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"聊天室";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightImageView.image = [UIImage imageNamed:@"baisesousuokuang-liaotianshi"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageView];
    
    UITapGestureRecognizer *rightTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchChatRoom:)];
    [rightImageView addGestureRecognizer:rightTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 搜索聊天室
- (void)searchChatRoom:(UITapGestureRecognizer *)tapGes
{
    ChatRoomSearchViewController *chatRoomSearchVC = [[ChatRoomSearchViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:chatRoomSearchVC];
    [self presentViewController:naVC animated:YES completion:nil];
}

#pragma mark - 建立数据源
- (void)addData
{
    // 模拟数据
    self. chatRoomDataArray = [NSMutableArray array];
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病", @"_name", @"本聊天室是为心脏病而建立", @"_introduce" , nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"心肌梗塞", @"_name", @"本聊天室是为心肌梗塞而建立", @"_introduce" , nil];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"糖尿病", @"_name", @"本聊天室是为糖尿病而建立", @"_introduce" , nil];
    NSArray *tempArray = [NSMutableArray arrayWithObjects:dic1, dic2, dic3, nil];
    for (NSDictionary *tempDic in tempArray) {
        ChatRoomModel *model = [[ChatRoomModel alloc] init];
        [model setValuesForKeysWithDictionary:tempDic];
        [self.chatRoomDataArray addObject:model];
    }

}

#pragma mark - 布局
- (void)addMySubview
{
    self.chatRoomTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44 * kAdjustParamater) style:UITableViewStylePlain];
    self.chatRoomTableView.backgroundColor = [UIColor whiteColor];
    self.chatRoomTableView.dataSource = self;
    self.chatRoomTableView.delegate = self;
    [self.view addSubview:_chatRoomTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.chatRoomDataArray.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * kAdjustParamater;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"chatRoomCell";
    ChatRoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ChatRoomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell.joinChatRoomButton addTarget:self action:@selector(JoinChat:) forControlEvents:UIControlEventTouchUpInside];
    cell.joinChatRoomButton.tag = indexPath.row + 100;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.chatRoomDataArray[indexPath.row];
    
    return cell;
}

#pragma mark - 加入聊天室
- (void)JoinChat:(UIButton *)button
{
    NSInteger row = button.tag - 100;
    ChatRoomModel *model = self.chatRoomDataArray[row];
    MyChatViewController *myChatVC = [[MyChatViewController alloc] init];
    myChatVC.conversationType = ConversationType_CHATROOM;
    myChatVC.title = model.name;
    [self.navigationController pushViewController:myChatVC animated:YES];
    NSLog(@"名字：%@", model.name);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ChatRoomModel *model = self.chatRoomDataArray[indexPath.row];
    
    MyChatViewController *myChatVC = [[MyChatViewController alloc] init];
    myChatVC.conversationType = ConversationType_CHATROOM;
    myChatVC.title = model.name;
    [self.navigationController pushViewController:myChatVC animated:YES];
    NSLog(@"名字：%@", model.name);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
